<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/1/17
 * Time: 17:40
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller;

use app\api\model\mysql\MallGoods;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use app\api\model\mysql\MallCate as ModelObj;
use think\facade\Log;

class Cate extends Base
{
    protected $modelObj = null;

    public function __construct(App $app)
    {
        $this->modelObj = new ModelObj();
        parent::__construct($app);
    }

    /**
     * @return \think\response\Json
     */
    public function getListInfo()
    {
        try {
            $lists = $this->modelObj
                ->field('id,title,image as url')
                ->where('pid',0)
                ->where('status', 1)
                ->order('sort desc,id desc')
                ->limit(8)
                ->select()->toArray();
        } catch (\Exception $e) {
            Log::error("cate_getListInfo:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "查询失败");
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }

    /**
     * 商城首页分类
     * @return \think\response\Json
     */
    public function shopCateList(){
        $cat_id =$this->request->param('cat_id',0,'intval');
        $child_id =$this->request->param('child_id',0,'intval');
        $page =$this->request->param('page',1,'intval');
        try {
            $where =[];
            $cats = $this->modelObj
                ->field('id,title,image as url')
                ->where("pid",$cat_id)
                ->order('sort desc,id desc')
                ->select()->toArray();
//            $catids=[];
//            if($cats){
//                foreach ($cats as $k=>$v){
//                    $catids[]=$v['id'];
//                }
//            }
//            $catids[]=$cat_id;
//            if($child_id>0){
//                $where[] =[['cate_id','FIND_IN_SET',$child_id]];
//            }else{
//                $where[] =[['cate_id','FIND_IN_SET',$cat_id]];
//            }
//            $where[] =[['status','=',1]];
            if($child_id>0){
                $cat_idss =$child_id;
            }else{
                $cat_idss =$cat_id;
            }
            $goods =(new MallGoods())->field('id,title,price,logo,cate_ids')->where("FIND_IN_SET($cat_idss,cate_ids) and status=1")->page($page,10)->select()->toArray();

            $pcatinfo =$this->modelObj->field('cat_img as url')->where("id",$cat_id)->find();
        } catch (\Exception $e) {
            Log::error("cate_getList:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, $e->getMessage());
        }

        return Show::json_show(Status::mini_normal, "ok", [
            'cats'=>$cats ?? [],
            'images'=>[$pcatinfo],
            'goods' =>$goods ?? []
        ]);
    }
}